import pandas as pd
from sklearn.neighbors import KNeighborsRegressor
from sklearn.preprocessing import MinMaxScaler
"""
K近邻算法_回归
"""

# 1.数据准备
x = [[39, 0, 31], [3, 2, 65], [2, 3, 55], [9, 38, 2], [8, 34, 17],
     [5, 2, 57], [39, 0, 31], [21, 17, 5], [45, 2, 9]]                 # 特征值的数据

y = [0.1, 0.2, 0.2, 0.3, 0.3, 0.2, 0.1, 0.1, 0.1]    # 预测的目标种类

# 2.对数据进行归一化(解决特征列单位相差太大的问题)
transformer = MinMaxScaler(feature_range=(0, 1))
transformer_x = transformer.fit_transform(x)
# print(transformer_x)

# 2.机器学习
# 2.1 使用算法构建模型
model = KNeighborsRegressor(n_neighbors=5)
#
# 2.2 训练模型
model.fit(transformer_x, y)

# 2.3 模型预测
x_predict = [[23, 3, 17]]                    # 预测数据
y_predict = model.predict(x_predict)             # 预测结果
print(f"预测的结果是：{y_predict}")